package gcb.metalsys.controller.cop;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import gcb.metalsys.enums.PriceEnum;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import gcb.metalsys.base.ResultUtil;
import gcb.metalsys.base.dto.ResultDTO;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import gcb.metalsys.service.cop.ProPriceService;
import gcb.metalsys.entity.cop.ProPrice;
import org.springframework.web.multipart.MultipartFile;

/**
 * @since 2020-07-15
 */
@RestController
@RequestMapping("/proPrice")
public class ProPriceController {

    @Autowired
    private ProPriceService proPriceService;

    @PostMapping("import")
    @ApiOperation(value = "导入")
    public ResultDTO importPrice(MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return ResultUtil.Error("500","文件为空!");
        }
        proPriceService.readExcel(file);
        return ResultUtil.Success();
    }

    @PostMapping("add")
    @ApiOperation(value = "增加")
    public ResultDTO addPrice(@RequestBody ProPrice proPrice) throws Exception {
//        String priceDate = proPrice.getPriceDate().format(DateTimeFormatter.ofPattern("yyyy.MM.dd"));
//        proPrice.setPriceData(priceDate);
        proPrice.setPriceStartTime("00:00:00");
        proPrice.setPriceEndTime("23:59:59");

        QueryWrapper<ProPrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("price_data",proPrice.getPriceData());
        queryWrapper.eq("product_number",proPrice.getProductNumber());
        queryWrapper.eq("price_type",proPrice.getPriceType());
        ProPrice existPrice = proPriceService.getOne(queryWrapper);

        if (existPrice != null) {
            return ResultUtil.Error("500","重复报价!");
        }

        proPriceService.save(proPrice);
        return ResultUtil.Success();
    }


    @GetMapping()
    @ApiOperation("查询、分页返回")
    public ResultDTO selectEntityPage(String productNumber,
                                        @RequestParam(value = "page", defaultValue = "0") int pageNum,
                                       @RequestParam(value = "limit", defaultValue = "8") int pageSize)throws Exception{
        QueryWrapper<ProPrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_number",productNumber);
        IPage<ProPrice> page = proPriceService.page(new Page<>(pageNum, pageSize), queryWrapper);
        List<ProPrice> data = page.getRecords();

        for (ProPrice datum : data) {
            for (PriceEnum value : PriceEnum.values()) {
                if (value.getCode().equals(datum.getPriceType())) {
                    datum.setPriceTypeName(value.getMessage());
                }
            }
        }

        return ResultUtil.Success(data,data.size());
    }

}
